package com.jyszkj.osmdroid

import android.content.Intent
import android.content.res.Resources
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.google.gson.Gson
import com.jyszkj.map.bean.DeviceBean
import com.jyszkj.map.constant.MapConstants
import com.jyszkj.map.listener.MapChangeListener
import com.jyszkj.map.listener.MapClickListener
import com.jyszkj.map.view.OsmdroidMapView
import com.jyszkj.osmdroid.bean.DataBean
import me.jessyan.autosize.AutoSizeCompat
import me.jessyan.autosize.internal.CancelAdapt
import me.jessyan.autosize.internal.CustomAdapt
import org.osmdroid.events.MapListener
import org.osmdroid.events.ScrollEvent
import org.osmdroid.events.ZoomEvent
import org.osmdroid.util.BoundingBox
import org.osmdroid.util.GeoPoint
import java.util.Timer
import java.util.TimerTask

class MainActivity : AppCompatActivity()  {
    private lateinit var mapView: OsmdroidMapView
    private lateinit var mapView2: OsmdroidMapView
    private lateinit var btn1: Button
    private lateinit var btn2: Button
    private var timer: Timer? = null
    private var selectIndex = 0
    private val list = mutableListOf(DataBean(116.02,-47.78,"南级洲海岭",4),
        DataBean(115.70,-47.70,"西印度洋",4),
        DataBean(-73.25,4.60,"哥伦比亚",4),
        DataBean(115.98,-47.80,"南级洲海岭2",4),
        DataBean(115.70,-47.70,"西印度洋",4),
        DataBean(75.13,41.07,"吉尔吉斯斯坦",4))
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        mapView = findViewById(R.id.map_view)
        mapView2 = findViewById(R.id.map_view2)
        btn1 = findViewById(R.id.btn_location1)
        btn2 = findViewById(R.id.btn_location2)
        initListener()
        init()
    }

    private fun init() {
        mapView.setMapOffset(0,0)
        mapView.showCurrentMaker(114.373,30.367,12.0)
        mapView.showShelterMarker(114.334,30.358,"1","123")
//        mapView.showShelterMarker(114.328,30.358,"2")
//        mapView.showShelterMarker(114.314,30.353,"3")
//        mapView.showShelterMarker(114.433,30.416,"4")
//        mapView.showShelterMarker(114.44,30.422,"5")
//        mapView.showShelterMarker(114.408,30.471,"6")

//        mapView.showCurrentMaker(114.207,35.105,37.6)
//        mapView.showShelterMarker(114.344,34.807,"1")
//        mapView.showShelterMarker(113.89,35.299,"2")
//        mapView.showShelterMarker(113.875,35.282,"3")
//        mapView.showShelterMarker(113.878,35.306,"4")
//        mapView.showShelterMarker(113.876,35.305,"5")
//        mapView.showShelterMarker(113.873,35.305,"6")

        //mapView.setCenter(114.207,35.105)
//        mapView.setCenter(114.207,35.105)
//        mapView.showDeviceMarker(DeviceBean("1","湖北省武汉市","YZX3132ACS000005",
//            114.344,34.807,"2","1","1","2"))
        mapView.setMapScrollable(true)
        mapView.setMultiTouchControls(true)
        mapView.setOnMapChangedListener(object : MapChangeListener{
            override fun onMapChanged() {
                Log.e("wangmeng","地图改变")
            }
        })
    }

    private fun calculateBoundingBox(points: List<GeoPoint>): BoundingBox {
        var minLat = points[0].latitude
        var maxLat = points[0].latitude
        var minLon = points[0].longitude
        var maxLon = points[0].longitude

        for (point in points) {
            minLat = minOf(minLat, point.latitude)
            maxLat = maxOf(maxLat, point.latitude)
            minLon = minOf(minLon, point.longitude)
            maxLon = maxOf(maxLon, point.longitude)
        }

        return BoundingBox(maxLat, maxLon, minLat, minLon)
    }

    private fun initListener() {
        mapView.setMapClickListener(object : MapClickListener {
            override fun onMapClick(lon: Double, lat: Double) {
                //Toast.makeText(this@MainActivity,"经度：$lon,纬度: $lat",Toast.LENGTH_SHORT).show()
            }

            override fun onMarkerClick(any: Any?) {
                Toast.makeText(this@MainActivity,"点击了：$any",Toast.LENGTH_SHORT).show()
            }

        })
        btn1.setOnClickListener {
            mapView.setCenter(114.344,35.807)
            //startTimer()
//            mapView.setMapScrollable(true)
//            mapView.showMarker(120.757,31.50772,2,"江苏省苏州市相城区",750)
        }
        btn2.setOnClickListener {
            stopTimer()
//            mapView.setMapScrollable(false)
//            mapView.showMarker(114.604,42.10,4,"河北省张家口市康保县",750)
        }
    }

    override fun getResources(): Resources {
        runOnUiThread {
            AutoSizeCompat.autoConvertDensityOfGlobal(super.getResources())
        }
        return super.getResources()
    }

    private fun startTimer() {
        if(timer != null){
            timer?.cancel()
            timer = null
        }
        timer = Timer()
        timer?.schedule(object : TimerTask(){
            override fun run() {
                runOnUiThread {
                    if(selectIndex >= list.size) {
                        selectIndex = 0
                    }
                    val bean = list[selectIndex]
                    mapView.showWarnMarker(bean.lon,bean.lat,bean.level,bean.address,3000)
                    selectIndex++
                }
            }

        },0,5*1000)
    }

    private fun stopTimer() {
        if(timer != null){
            timer?.cancel()
            timer = null
        }
    }

}